import axios from "axios"
import { useCustomersStore } from "../store"

// const baseURL = "http://182.92.162.156:8083"
const baseURL = "/api"

const request = axios.create({
    baseURL: baseURL,
    timeout: 5000,
    withCredentials: false,
    headers:{
      "Content-Type":"application/json;charset=utf-8"
    }
})

//拦截器

//请求拦截器  发请求-拦截器-服务器

request.interceptors.request.use(

  (config) => {
    
    const customerstore = useCustomersStore()
   // 请求头添加token
   console.log(customerstore.token,"请求拦截器")
   config.headers["token"] =customerstore.token

   // config.headers["Content-Type"] = "application/json"

   return config;

  },

  (error) => {

   Promise.reject(error);

  }
 
 );
 

 // 响应拦截器  服务器-拦截器-页面

 request.interceptors.response.use(

    (response) => {
   
     const res = response.data;
     if (res.code !== "200"){
       // ElMessage({
       //   message: res.message + ': 错误状态' + res.code,
       //   type: 'error',
       // })
     }
     // response.setHeader("Access-Control-Allow-Origin", "http://www.domain1.com");
     return res;
   
    },
   
    (error) => {
   
     console.log(error)
   
    }
   
   );
   export default request